
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>pymatgen.util.num &#8212; pymatgen 2020.7.3 documentation</title>
    <link rel="stylesheet" href="../../../_static/basic.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script src="../../../_static/jquery.js"></script>
    <script src="../../../_static/underscore.js"></script>
    <script src="../../../_static/doctools.js"></script>
    <script src="../../../_static/language_data.js"></script>
    <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
 
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-33990148-1']);
  _gaq.push(['_trackPageview']);
</script>

  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../pymatgen.html" accesskey="U">pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.util.num</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
          <div class="body" role="main">
            
  <h1>Source code for pymatgen.util.num</h1><div class="highlight"><pre>
<span></span><span class="c1"># coding: utf-8</span>
<span class="c1"># Copyright (c) Pymatgen Development Team.</span>
<span class="c1"># Distributed under the terms of the MIT License.</span>


<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">This module provides utilities for basic math operations.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">collections</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>


<div class="viewcode-block" id="abs_cap"><a class="viewcode-back" href="../../../pymatgen.util.num.html#pymatgen.util.num.abs_cap">[docs]</a><span class="k">def</span> <span class="nf">abs_cap</span><span class="p">(</span><span class="n">val</span><span class="p">,</span> <span class="n">max_abs_val</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns the value with its absolute value capped at max_abs_val.</span>
<span class="sd">    Particularly useful in passing values to trignometric functions where</span>
<span class="sd">    numerical errors may result in an argument &gt; 1 being passed in.</span>

<span class="sd">    Args:</span>
<span class="sd">        val (float): Input value.</span>
<span class="sd">        max_abs_val (float): The maximum absolute value for val. Defaults to 1.</span>

<span class="sd">    Returns:</span>
<span class="sd">        val if abs(val) &lt; 1 else sign of val * max_abs_val.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="nb">max</span><span class="p">(</span><span class="nb">min</span><span class="p">(</span><span class="n">val</span><span class="p">,</span> <span class="n">max_abs_val</span><span class="p">),</span> <span class="o">-</span><span class="n">max_abs_val</span><span class="p">)</span></div>


<div class="viewcode-block" id="sort_dict"><a class="viewcode-back" href="../../../pymatgen.util.num.html#pymatgen.util.num.sort_dict">[docs]</a><span class="k">def</span> <span class="nf">sort_dict</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">reverse</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Sorts a dict by value.</span>

<span class="sd">    Args:</span>
<span class="sd">        d: Input dictionary</span>
<span class="sd">        key: Function which takes an tuple (key, object) and returns a value to</span>
<span class="sd">            compare and sort by. By default, the function compares the values</span>
<span class="sd">            of the dict i.e. key = lambda t : t[1]</span>
<span class="sd">        reverse: Allows to reverse sort order.</span>

<span class="sd">    Returns:</span>
<span class="sd">        OrderedDict object whose keys are ordered according to their value.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">kv_items</span> <span class="o">=</span> <span class="p">[</span><span class="n">kv</span> <span class="k">for</span> <span class="n">kv</span> <span class="ow">in</span> <span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">()]</span>

    <span class="c1"># Sort kv_items according to key.</span>
    <span class="k">if</span> <span class="n">key</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">kv_items</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">t</span><span class="p">:</span> <span class="n">t</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">reverse</span><span class="o">=</span><span class="n">reverse</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">kv_items</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">key</span><span class="o">=</span><span class="n">key</span><span class="p">,</span> <span class="n">reverse</span><span class="o">=</span><span class="n">reverse</span><span class="p">)</span>

    <span class="c1"># Build ordered dict.</span>
    <span class="k">return</span> <span class="n">collections</span><span class="o">.</span><span class="n">OrderedDict</span><span class="p">(</span><span class="n">kv_items</span><span class="p">)</span></div>


<div class="viewcode-block" id="minloc"><a class="viewcode-back" href="../../../pymatgen.util.num.html#pymatgen.util.num.minloc">[docs]</a><span class="k">def</span> <span class="nf">minloc</span><span class="p">(</span><span class="n">seq</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return the index of the (first) minimum in seq</span>

<span class="sd">    &gt;&gt;&gt; assert minloc(range(3)) == 0</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="nb">min</span><span class="p">(</span><span class="nb">enumerate</span><span class="p">(</span><span class="n">seq</span><span class="p">),</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span><span class="p">[</span><span class="mi">1</span><span class="p">])[</span><span class="mi">0</span><span class="p">]</span></div>


<div class="viewcode-block" id="maxloc"><a class="viewcode-back" href="../../../pymatgen.util.num.html#pymatgen.util.num.maxloc">[docs]</a><span class="k">def</span> <span class="nf">maxloc</span><span class="p">(</span><span class="n">seq</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return the index of the (first) maximum in seq</span>

<span class="sd">    &gt;&gt;&gt; assert maxloc([1,3,2,3]) == 1</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="nb">max</span><span class="p">(</span><span class="nb">enumerate</span><span class="p">(</span><span class="n">seq</span><span class="p">),</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span><span class="p">[</span><span class="mi">1</span><span class="p">])[</span><span class="mi">0</span><span class="p">]</span></div>


<div class="viewcode-block" id="min_max_indexes"><a class="viewcode-back" href="../../../pymatgen.util.num.html#pymatgen.util.num.min_max_indexes">[docs]</a><span class="k">def</span> <span class="nf">min_max_indexes</span><span class="p">(</span><span class="n">seq</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Uses enumerate, max, and min to return the indices of the values</span>
<span class="sd">    in a list with the maximum and minimum value:</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">l</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="nb">enumerate</span><span class="p">(</span><span class="n">seq</span><span class="p">),</span> <span class="n">key</span><span class="o">=</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
    <span class="k">return</span> <span class="n">l</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">],</span> <span class="n">l</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span></div>


<div class="viewcode-block" id="strictly_increasing"><a class="viewcode-back" href="../../../pymatgen.util.num.html#pymatgen.util.num.strictly_increasing">[docs]</a><span class="k">def</span> <span class="nf">strictly_increasing</span><span class="p">(</span><span class="n">values</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;True if values are stricly increasing.&quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="nb">all</span><span class="p">(</span><span class="n">x</span> <span class="o">&lt;</span> <span class="n">y</span> <span class="k">for</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">values</span><span class="p">,</span> <span class="n">values</span><span class="p">[</span><span class="mi">1</span><span class="p">:]))</span></div>


<div class="viewcode-block" id="strictly_decreasing"><a class="viewcode-back" href="../../../pymatgen.util.num.html#pymatgen.util.num.strictly_decreasing">[docs]</a><span class="k">def</span> <span class="nf">strictly_decreasing</span><span class="p">(</span><span class="n">values</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;True if values are stricly decreasing.&quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="nb">all</span><span class="p">(</span><span class="n">x</span> <span class="o">&gt;</span> <span class="n">y</span> <span class="k">for</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">values</span><span class="p">,</span> <span class="n">values</span><span class="p">[</span><span class="mi">1</span><span class="p">:]))</span></div>


<div class="viewcode-block" id="non_increasing"><a class="viewcode-back" href="../../../pymatgen.util.num.html#pymatgen.util.num.non_increasing">[docs]</a><span class="k">def</span> <span class="nf">non_increasing</span><span class="p">(</span><span class="n">values</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;True if values are not increasing.&quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="nb">all</span><span class="p">(</span><span class="n">x</span> <span class="o">&gt;=</span> <span class="n">y</span> <span class="k">for</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">values</span><span class="p">,</span> <span class="n">values</span><span class="p">[</span><span class="mi">1</span><span class="p">:]))</span></div>


<div class="viewcode-block" id="non_decreasing"><a class="viewcode-back" href="../../../pymatgen.util.num.html#pymatgen.util.num.non_decreasing">[docs]</a><span class="k">def</span> <span class="nf">non_decreasing</span><span class="p">(</span><span class="n">values</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;True if values are not decreasing.&quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="nb">all</span><span class="p">(</span><span class="n">x</span> <span class="o">&lt;=</span> <span class="n">y</span> <span class="k">for</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">values</span><span class="p">,</span> <span class="n">values</span><span class="p">[</span><span class="mi">1</span><span class="p">:]))</span></div>


<div class="viewcode-block" id="monotonic"><a class="viewcode-back" href="../../../pymatgen.util.num.html#pymatgen.util.num.monotonic">[docs]</a><span class="k">def</span> <span class="nf">monotonic</span><span class="p">(</span><span class="n">values</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s2">&quot;&lt;&quot;</span><span class="p">,</span> <span class="n">atol</span><span class="o">=</span><span class="mf">1.e-8</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns False if values are not monotonic (decreasing|increasing).</span>
<span class="sd">    mode is &quot;&lt;&quot; for a decreasing sequence, &quot;&gt;&quot; for an increasing sequence.</span>
<span class="sd">    Two numbers are considered equal if they differ less that atol.</span>

<span class="sd">    .. warning:</span>
<span class="sd">        Not very efficient for large data sets.</span>

<span class="sd">    &gt;&gt;&gt; values = [1.2, 1.3, 1.4]</span>
<span class="sd">    &gt;&gt;&gt; monotonic(values, mode=&quot;&lt;&quot;)</span>
<span class="sd">    False</span>
<span class="sd">    &gt;&gt;&gt; monotonic(values, mode=&quot;&gt;&quot;)</span>
<span class="sd">    True</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">values</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="k">if</span> <span class="n">mode</span> <span class="o">==</span> <span class="s2">&quot;&gt;&quot;</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">values</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">):</span>
            <span class="n">v</span><span class="p">,</span> <span class="n">vp</span> <span class="o">=</span> <span class="n">values</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">values</span><span class="p">[</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span>
            <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">vp</span> <span class="o">-</span> <span class="n">v</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">atol</span> <span class="ow">and</span> <span class="n">vp</span> <span class="o">&lt;=</span> <span class="n">v</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">False</span>

    <span class="k">elif</span> <span class="n">mode</span> <span class="o">==</span> <span class="s2">&quot;&lt;&quot;</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">values</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">):</span>
            <span class="n">v</span><span class="p">,</span> <span class="n">vp</span> <span class="o">=</span> <span class="n">values</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">values</span><span class="p">[</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span>
            <span class="k">if</span> <span class="nb">abs</span><span class="p">(</span><span class="n">vp</span> <span class="o">-</span> <span class="n">v</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">atol</span> <span class="ow">and</span> <span class="n">vp</span> <span class="o">&gt;=</span> <span class="n">v</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">False</span>

    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Wrong mode </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="nb">str</span><span class="p">(</span><span class="n">mode</span><span class="p">))</span>

    <span class="k">return</span> <span class="kc">True</span></div>


<div class="viewcode-block" id="round_to_sigfigs"><a class="viewcode-back" href="../../../pymatgen.util.num.html#pymatgen.util.num.round_to_sigfigs">[docs]</a><span class="k">def</span> <span class="nf">round_to_sigfigs</span><span class="p">(</span><span class="n">num</span><span class="p">,</span> <span class="n">sigfigs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Rounds a number rounded to a specific number of significant</span>
<span class="sd">    figures instead of to a specific precision.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">sigfigs</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">int</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s2">&quot;Number of significant figures must be integer.&quot;</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">sigfigs</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Number of significant figures &quot;</span>
                         <span class="s2">&quot;must be larger than zero.&quot;</span><span class="p">)</span>
    <span class="k">elif</span> <span class="n">num</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">num</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">prec</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">sigfigs</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">ceil</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">absolute</span><span class="p">(</span><span class="n">num</span><span class="p">))))</span>
        <span class="k">return</span> <span class="nb">round</span><span class="p">(</span><span class="n">num</span><span class="p">,</span> <span class="n">prec</span><span class="p">)</span></div>


<div class="viewcode-block" id="make_symmetric_matrix_from_upper_tri"><a class="viewcode-back" href="../../../pymatgen.util.num.html#pymatgen.util.num.make_symmetric_matrix_from_upper_tri">[docs]</a><span class="k">def</span> <span class="nf">make_symmetric_matrix_from_upper_tri</span><span class="p">(</span><span class="n">val</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Given a symmetric matrix in upper triangular matrix form as flat array indexes as:</span>
<span class="sd">    [A_xx,A_yy,A_zz,A_xy,A_xz,A_yz]</span>
<span class="sd">    This will generate the full matrix:</span>
<span class="sd">    [[A_xx,A_xy,A_xz],[A_xy,A_yy,A_yz],[A_xz,A_yz,A_zz]</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">idx</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span>
    <span class="n">val</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">val</span><span class="p">)[</span><span class="n">idx</span><span class="p">]</span>
    <span class="n">mask</span> <span class="o">=</span> <span class="o">~</span><span class="n">np</span><span class="o">.</span><span class="n">tri</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">k</span><span class="o">=-</span><span class="mi">1</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">bool</span><span class="p">)</span>
    <span class="n">out</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">val</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
    <span class="n">out</span><span class="p">[</span><span class="n">mask</span><span class="p">]</span> <span class="o">=</span> <span class="n">val</span>
    <span class="n">out</span><span class="o">.</span><span class="n">T</span><span class="p">[</span><span class="n">mask</span><span class="p">]</span> <span class="o">=</span> <span class="n">val</span>
    <span class="k">return</span> <span class="n">out</span></div>


<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">&quot;__main__&quot;</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">doctest</span>

    <span class="n">doctest</span><span class="o">.</span><span class="n">testmod</span><span class="p">()</span>
</pre></div>

            <div class="clearer"></div>
          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../pymatgen.html" >pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.util.num</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
        &#169; Copyright 2011, Pymatgen Development Team.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.1.2.
    </div>
<div class="footer">This page uses <a href="http://analytics.google.com/">
Google Analytics</a> to collect statistics. You can disable it by blocking
the JavaScript coming from www.google-analytics.com.
<script type="text/javascript">
  (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ?
              'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
  })();
</script>
</div>

  </body>
</html>